class Counter{
    private int count=100000;
    private Object locker=new Object();
    public void add(){
        synchronized (locker){
            count++;
        }
    }
    public void sub(){
        count--;
    }
    public int get(){
        return count;
    }
}
public class demo01 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter=new Counter();
        //两个线程分别对这个counter自增50000次
        Thread t1=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.sub();
            }
        });
        Thread t2=new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.sub();
            }
        });
        t1.start();
        t2.start();
        //等待两个线程结束看结果
        t1.join();
        t2.join();
        System.out.println(counter.get());
    }
}
